import urllib.request
import time
import ssl
import json
import string
 
 
while True:
 
    # def talk():
    target = r'http://api.qingyunke.com/api.php?key=free&appid=0&msg='
    print("================================")
    keyword = input(">>")
    if keyword == "退出":
        print("不聊算了,拜拜")
        break
    tmp = target + keyword
    url = urllib.parse.quote(tmp, safe=string.printable)
    page = urllib.request.urlopen(url)
    #print(url)
 
    # url = urllib.parse.quote(tmp,safe=string.printable)
    # url = url1.read().decode("utf-8")
    html = page.read().decode("utf-8")
    # print(html)
    # print(str(page))
 
    # page = urllib.request.urlopen(url)
    # html = page.read()
 
    # print(html)
    res = json.loads(html)#json转为dict,json.loads 用于解码 JSON 数据。该函数返回 Python 字段的数据类型
    # print(res)
    #return res
    # res = json.loads(html)
    # b = json.dumps(res, ensure_ascii=False, indent=4)
    # print(b)
    # dict_talk = json.loads(b)
    # print(dict_talk)
    # return dict_talk
    # a = res
    print(res['content'])
 
# if __name__ == "__main__":
#     while True:
#         a = talk()
#         print(a['content'])
# print(a)
# answer = urllib.parse.unquote(a)
# answer = json.dumps(answer, ensure_ascii=False, indent=4)
# res = json.loads(answer)
# print(answer)
    # print()